package MemberService

import (
	"context"
	"gdshop-font-api-go/app/request/MemberReq"
	"gdshop-font-api-go/library/response"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
)

func MySearchs(ctx context.Context, parames *MemberReq.MySearchs) *response.JsonResponse {
	configId := "shop_config"
	value, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "sys_config").Where("config_key", configId).Value("config_value")
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	if value.IsEmpty() {
		return response.SuccessByRequestMessageData(nil, "成功", g.Map{})
	}
	json, err := gjson.LoadContent(value)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	fanhui := g.Map{}
	if json.Contains("hotSearchs") {
		fanhui["hotSearchs"] = json.Get("hotSearchs")
	} else {
		fanhui["hotSearchs"] = []string{}
	}
	array, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_search").Where("member_id",
		parames.MemberId).Fields("keyword").Array()
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	fanhui["mySearchs"] = array

	return response.SuccessByRequestMessageData(nil, "成功", fanhui)
}
